<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 练习-带选项的用户脚本 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 练习-带选项的用户脚本</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>要求如下：</p>
<ul>
<li>只支持三个选项 ‘–del’ ‘–add’ –help输入其他选项报错。</li>
<li>使用‘–add’需要验证用户名是否存在，存在则反馈存在。且不添加。 不存在则创建该用户，切添加与该用户名相同的密码。并且反馈。</li>
<li>使用‘–del’ 需要验证用户名是否存在，存在则删除用户及其家目录。不存在则反馈该用户不存在。</li>
<li>–help 选项反馈出使用方法</li>
<li>支持以，分隔   一次删除多个或者添加多个用户。</li>
<li>能用echo $?  检测脚本执行情况  成功删除或者添加为0,报错信息为其他数字。</li>
<li>能以，分割。一次性添加或者 删除多个用户。  例如 adddel.sh –add user1,user2,user3…….</li>
<li>不允许存在明显bug。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#written by aming.</span></div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -eq 0 -o <span class="variable">$#</span> -gt 2 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">    <span class="built_in">echo</span> <span class="string">"use <span class="variable">$0</span> --add username or <span class="variable">$0</span> --del username or <span class="variable">$0</span> --help."</span></div><div class="line">    <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="keyword">case</span> <span class="variable">$1</span> <span class="keyword">in</span></div><div class="line">    --add)</div><div class="line">        n=0</div><div class="line">        <span class="keyword">for</span> u <span class="keyword">in</span> `<span class="built_in">echo</span> <span class="variable">$2</span>|sed <span class="string">'s/,/ /g'</span>`; <span class="keyword">do</span></div><div class="line">            <span class="keyword">if</span> awk -F: <span class="string">'&#123;print $1&#125;'</span> /etc/passwd |grep -qw <span class="string">"<span class="variable">$u</span>"</span></div><div class="line">            <span class="keyword">then</span></div><div class="line">                <span class="built_in">echo</span> <span class="string">"The user <span class="variable">$u</span> exist."</span></div><div class="line">            <span class="keyword">else</span></div><div class="line">                useradd <span class="variable">$u</span></div><div class="line">                <span class="built_in">echo</span> -e <span class="string">"<span class="variable">$u</span>\n<span class="variable">$u</span>"</span>|passwd <span class="variable">$u</span> &gt;/dev/null 2&gt;&amp;1</div><div class="line">                <span class="built_in">echo</span> <span class="string">"The user <span class="variable">$u</span> added successfully."</span></div><div class="line">                n=$[<span class="variable">$n</span>+1]</div><div class="line">            <span class="keyword">fi</span></div><div class="line">        <span class="keyword">done</span></div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$n</span> -eq 0 ]; <span class="keyword">then</span></div><div class="line">            <span class="built_in">exit</span> 2</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        ;;</div><div class="line">    --del)</div><div class="line">        n=0</div><div class="line">        <span class="keyword">for</span> u <span class="keyword">in</span> `<span class="built_in">echo</span> <span class="variable">$2</span>|sed <span class="string">'s/,/ /g'</span>`; <span class="keyword">do</span></div><div class="line">            <span class="keyword">if</span> awk -F: <span class="string">'&#123;print $1&#125;'</span> /etc/passwd|grep -qw <span class="string">"<span class="variable">$u</span>"</span></div><div class="line">            <span class="keyword">then</span></div><div class="line">                userdel -r <span class="variable">$u</span></div><div class="line">                <span class="built_in">echo</span> <span class="string">"The user <span class="variable">$u</span> deleted successfully."</span></div><div class="line">                n=$[<span class="variable">$n</span>+1]</div><div class="line">            <span class="keyword">else</span></div><div class="line">                <span class="built_in">echo</span> <span class="string">"The user <span class="variable">$u</span> not exist."</span></div><div class="line">            <span class="keyword">fi</span></div><div class="line">        <span class="keyword">done</span></div><div class="line">        <span class="keyword">if</span> [ <span class="variable">$n</span> -eq 0 ]; <span class="keyword">then</span></div><div class="line">                <span class="built_in">exit</span> 3</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        ;;</div><div class="line">    --<span class="built_in">help</span>)</div><div class="line">        <span class="built_in">echo</span> -e <span class="string">"--add can add user,and the passwd is the same as username.     </span></div><div class="line"><span class="string">    It can add multiuser such as --add user1,user2,user3..."</span></div><div class="line">        <span class="built_in">echo</span> <span class="string">"--del cat delete user.It can delete user such as --del user1,user2,user3..."</span></div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"use <span class="variable">$0</span> --add username or <span class="variable">$0</span> --del username or <span class="variable">$0</span> --help."</span></div><div class="line">        <span class="built_in">exit</span> 1</div><div class="line">        ;;</div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Shell 练习/34. shell 练习-带选项的用户脚本/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
